//
//  MainViewController.swift
//  SinaWeibo
//
//  Created by 林柱强 on 16/1/28.
//  Copyright © 2016年 林柱强. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    @objc private func composeDidClick(){
        let compose = ComposeViewController()
        let nav = UINavigationController(rootViewController: compose)
        presentViewController(nav, animated: true, completion: nil)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        //由于 tabBar是只读，不能够直接操作，要修改，可以使用MVC
        let mainTabBar = MainTabBar()
        //KVC 赋值
        setValue(mainTabBar, forKey: "tabBar")
        //添加子视图控制器
        addChildViewControllers()
        
        //设置点击事件
        mainTabBar.composeBtn.addTarget(self, action: "composeDidClick", forControlEvents: .TouchUpInside)
    }
    
    private func addChildViewControllers() {
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
    }
    
    private func addChildViewController(vc: UIViewController, title : String, imageName: String) {
        //实例化导航视图控制器
        let nav = UINavigationController(rootViewController: vc)

        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
//        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")?.imageWithRenderingMode(.AlwaysOriginal)
//        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: .Selected)
        
        //添加导航视图
        addChildViewController(nav)
    }
}
